#pragma once

#include "NetTypes.h"
#include "Connection.h"
#include "BitStream.h"
#include "Socket.h"

namespace phynet{
namespace net{

struct MessageInfo
{
	int m_type;
	BitStream* m_data;
	Address m_sender;
};
typedef std::vector<MessageInfo> MessageInfos;

class TransportLayer
{
public:
	TransportLayer();
	~TransportLayer();
	
	bool Open(int i_port);
	void Close(bool i_hardDisconnect = false);

	void Update(MessageInfos& o_infos);

	bool SendPacket(const Address& i_dest, int i_channelID, int i_type, const BitStream* i_data);
	bool BroadcastPacket(int i_channelID, int i_type, const BitStream* i_data);

private:

	void HandleIncomingMessage(MessageInfos& o_infos, const Address& i_addr, const char* i_message, const UInt i_messageSize);

	void FlushMessages()
	{
		//TODO
	}

	static const int NUM_READINGS = 5;
	static const int NUM_WRITINGS = 5;

	Connections m_connections;
	Socket m_socket;

	int m_status;
};

}} //namespaces